<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/1/17
 * Time: 10:44
 */
Class ImageGroupModel extends Model{

    /**
     * 获得单条数据
     * @param $condition 条件
     * @return mixed
     */
    public function getDataByOne($condition,$field){
        return $this->where($condition)->field($field)->find();
    }

    /**
     * 获得多条数据
     * @param $condition 条件
     * @param $field 字段
     * @param $order 排序
     * @param $limit 数量
     * @return mixed
     */
    public function getDataByMore($condition,$field,$order,$limit){
        return $this->where($condition)->field($field)->order($order)->limit($limit)->select();
    }

    /**
     * 更新数据
     * @param $condition 条件
     * @param $data 数据
     * @return mixed
     */
    public function updataImageGroup($condition,$data){
          return $this->where($condition)->save($data);
    }

    /**
     * 非空分类的删除
     * @param $id 删除的id
     * @return array|mixed
     */
    public function delImageGroup($id){
          $data = $this->getDataByMore(array("pid"=>$id));
          if(!empty($data)){
               return array(
                   "code" => 403,
                   "msg" => "该分类不为空",
               );
               exit();
          }
          $res = $this->delete($id);
          if($res){
              return array(
                  "code" => 200,
                  "msg" => "删除成功",
              );
          }else{
              return array(
                  "code" => 403,
                  "msg" => "删除失败",
              );
          }
    }

    /**
     * 添加评论
     * @param $data 数据
     * @return mixed
     */
    Public function addImageGroup($data){
        return $this->add($data);
    }

    /**
     * 返回多维数组的数据
     * @return array
     */
    public function getCategory(){
        $data = self::getDataByMore(); // 获取所有分类
        import("Class.Category",APP_PATH); // 引入无限极分类库
        $category = new Category(); // 实例化类
        $formatdata = $category::unLimitdForLevel($data); //调方法
        return $formatdata;
    }

    /**
     * 循环数据得到图片
     * @param $data
     * @return array
     */
    public function getImageData($data){
         $count = 5;
         if(is_array($data)&&!empty($data)) {
             foreach ($data as $key => $v){
                   $where = array("cid"=>$v['id']);
                   $count = M('Image')->where($where)->count();
                   if(!empty($count)){
                          $res = D('Image')->getDataByMore($where,'','',$count);
                          $data[$key]['count'] = $count;
                          $data[$key]['imageArr'] =  $res;
                   }
             }
         }
         return $data;
    }
}